package com.sunlong.jiangwei.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sunlong.jiangwei.util.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Transient;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@Document(indexName = "jiangwei",type = "jw_spu")
public class JwSpu implements Serializable {


    /**
     * spu id
     */
    @Id
    @org.springframework.data.annotation.Id
    @KeySql(useGeneratedKeys = true)
    private Long spuId;

    /**
     * 标题
     */
    private String spuTitle;

    /**
     * 子标题
     */
    private String spuSubTitle;

    /**
     * 1级类目id
     */
    private Long categoryId;

    /**
     * 商品所属品牌id
     */
    private Long storeId;

    /**
     * 是否上架，0下架，1上架
     */
    private Boolean spuSaleable;

    /**
     * 是否有效，0已删除，1有效
     */
    private Boolean spuValid;

    /**
     * 添加时间
     */
    private Date spuCreateTime;
    @Transient
    private String spuCreateTimeStr;

    /**
     * 最后修改时间
     */
    private Date spuLastUpdateTime;
    /**
     * 最低价格
     */
    @Transient
    private Long minPrice;
    /**
     * 商品缩略展示图片
     */
    @Transient
    private String imageUrl;

    @Transient
    private String storeName;

    @Transient
    private String categoryName;

    @Transient
    //@Field(store = false)
    private List<JwSku> skus;

    @Transient
    //@Field(store = false)
    private JwCategory category;

    @Transient
    //@Field(store = false)
    private JwStore store;

    @Transient
    //@Field(store = false)
    private JwSpuDetail spuDetail;


    public String getSpuCreateTimeStr() {
        return DateUtil.format(spuCreateTime);
    }

    @NotNull
    public static JwSpu getJwSpu(Map<String, Object> spuMap) {
        JwSpu spu = new JwSpu();
        if (null != spuMap.get("spuId")) {
            spu.setSpuId(((Integer) spuMap.get("spuId")).longValue());
        }
        spu.setStoreId(Integer.toUnsignedLong((Integer) spuMap.get("storeId")));
        spu.setSpuTitle((String) spuMap.get("spuTitle"));
        spu.setSpuSubTitle((String) spuMap.get("spuSubTitle"));
        spu.setCategoryId(Integer.toUnsignedLong((Integer) spuMap.get("categoryId")));
        return spu;
    }
}
